home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / kbhit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-18  |  1.5 KB  |  64 lines  |  [TEXT/KAHL]

  1. /*
  2. kbhit.c
  3. kbhit() returns 1 if a keypress awaits processing, 0 otherwise.
  4. getcharUnbuffered() always returns immediately. It returns value -1 if there's 
  5. no character to return.
  6.  
  7. getcharUnbuffered() is used by Choose.c
  8.  
  9. 6/13/90    dgp wrote kbhit, based on suggestion from Michael Kahl and an earlier version by
  10.         Evan Relkin.
  11. 12/25/93 dgp cosmetic editing
  12. 8/1/94 dgp added support for MetroWerks CodeWarrior C 3.5.
  13. 10/1/94 dgp updated for MetroWerks CodeWarrior C 4.5, which still doesn't support
  14.         unbuffered input from the console.
  15. 10/8/94    dgp Discovered that GetNextEvent() works fine for unbuffered input in CodeWarrior C.
  16. */
  17. #include "VideoToolbox.h"
  18. #if (THINK_C || THINK_CPLUS)
  19.     #include <console.h>
  20. #endif
  21. #if __MWERKS__
  22.     #include <console.h>
  23. #endif
  24.  
  25. int kbhit(void)
  26. {
  27.     #if (THINK_C || THINK_CPLUS)
  28.         int c;
  29.     
  30.         c=getcharUnbuffered();
  31.         if(c==EOF)return 0;
  32.         ungetc(c,stdin);
  33.         return 1;
  34.     #else
  35.         EventRecord event;
  36.         
  37.         return EventAvail(keyDownMask,&event);
  38.     #endif
  39. }
  40.  
  41. int getcharUnbuffered(void)
  42. {
  43.     #if (THINK_C || THINK_CPLUS)
  44.         int c;
  45.  
  46.         csetmode(C_RAW,stdin);        /* unbuffered: no echo, no editing */
  47.         c=getchar();
  48.         csetmode(C_ECHO,stdin);        /* default mode: line-buffered, echo, full editing */
  49.     #else
  50.         int c;
  51.         EventRecord event;
  52.         #if __MWERKS__
  53.             extern long __SIOUXtextWindow;
  54.     
  55.             SelectWindow((WindowPtr)__SIOUXtextWindow);
  56.         #endif
  57.         while(!GetNextEvent(keyDownMask,&event)) ;
  58.         if((event.message&charCodeMask)=='.' && (event.modifiers&cmdKey)) abort();
  59.         c=event.message&charCodeMask;
  60.     #endif
  61.     return c;
  62. }
  63.  
  64.